package org.mineground.commands.player;

import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.mineground.Main;
import org.mineground.PermissionNodes;
import org.mineground.player.MinegroundPlayer;
import org.mineground.world.WarpLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @file Home.java (01.03.2012)
 * @author Daniel Koenen
 *
 */
public class Home implements CommandExecutor {
    private static final Logger ExceptionLogger = LoggerFactory.getLogger(Home.class);
    
    @Override
    public boolean onCommand(CommandSender cs, Command cmnd, String string, String[] arguments) {
        Player player = (Player) cs;
        
        if (!player.hasPermission(PermissionNodes.PERMISSION_HOME_COMMAND)) {
            return true;
        }
        
        if (arguments.length == 1 && arguments[0].equalsIgnoreCase("set")) {
            MinegroundPlayer playerInstance = Main.getInstance().getPlayer(player);
            Location homeLocation = player.getLocation();
            
            if (playerInstance == null) {
                player.sendMessage(ChatColor.RED + "Fatal Error: " + ChatColor.WHITE + "Your profile data hasn't been loaded, try to reconnect.");
		return true;
	    }
            
            if (playerInstance.getHomeId() != -1 && WarpLoader.warps.containsKey(playerInstance.getHomeId())) {
                WarpLoader.remove(WarpLoader.warps.get(playerInstance.getHomeId()), player);
                playerInstance.setHomeId(-1);
            }
            
            int warpId = -1;
            
            try {
                warpId = WarpLoader.add(playerInstance, homeLocation.getBlockX(), homeLocation.getBlockY(), homeLocation.getBlockZ(), warpId, warpId, player.getWorld().getName(), "home_" + player.getName().hashCode(), 0, false);
            }
            
            catch (Exception exception) {
                ExceptionLogger.error("Exception caught", exception);
            }
            
            
            if (warpId == -1) {
                player.sendMessage(ChatColor.RED + "* Error: An unknown error occured when attempting to create your home spawn, please contact an administrator.");
                return true;
            }
            
            playerInstance.setHomeId(warpId);
            player.sendMessage(ChatColor.DARK_GREEN + "Your home warp has been set, use '/home' to get there.");
            return true;
        }
        
        int warpId = Main.getInstance().getPlayer(player).getHomeId();
        
        if (warpId == -1) {
            player.sendMessage(ChatColor.RED + "* INFO: You haven't set a home yet, please do so by using '/home set <WarpId>'.");
            return true;
        }
        
        org.mineground.world.Warp playerHome = WarpLoader.warps.get(warpId);
        
        if (playerHome == null) {
            player.sendMessage(ChatColor.RED + "* Error: An unknown error occured when attempting to warp to your home spawn, please contact an administrator.");
            return true;
        }
        
        if (playerHome.getWorld().equalsIgnoreCase(Main.getInstance().getConfigHandler().getSurvivalWorldName())) {
            Location homeLocation = new Location(Main.getInstance().getServer().getWorld(Main.getInstance().getConfigHandler().getSurvivalWorldName()), playerHome.getX(), playerHome.getY(), playerHome.getZ(), playerHome.getYaw(), playerHome.getPitch());
            Main.getInstance().getPlayer(player).initializeSurvivalMovementDelay(homeLocation);
            return true;
        }
        
        playerHome.teleport(player);
        return true;
    }

}
